package com.usian.search.listener;

import com.usian.search.service.ApArticleSearchService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Optional;

import static com.usian.common.constants.message.Article2EsConstants.ARTICLE_2_ES_TOPIC;

/**
 * 文章审核通过监听类，同步文章数据到索引库
 */
@Component
public class ArticleListener {

    @Autowired
    private ApArticleSearchService apArticleSearchService;

    @KafkaListener(topics = ARTICLE_2_ES_TOPIC)
    public void receiveMessage(ConsumerRecord<?,?> record){
        Optional<? extends ConsumerRecord<?, ?>> optional = Optional.ofNullable(record);
        if(optional.isPresent()){//判断消息是否存在
            //获取消息内容
            Object value = record.value();
            Long articleId=Long.parseLong(value.toString());
            //同步App文章到索引库
            apArticleSearchService.ImportOneArticle2Es(articleId);
        }
    }
}
